package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.activity.stage.dto.request.prize.UseLoanCashBackReqDTO;
import com.niiwoo.activity.stage.dto.response.prize.UserPrizeResponseDTO;
import com.niiwoo.activity.stage.enums.prize.UserPrizeTypeIdEnum;
import com.niiwoo.activity.stage.enums.prize.UserPrizeUseSceneEnum;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.civet.base.enums.RefundTypeEnum;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectBorrowerSummary;
import com.niiwoo.civet.trade.dao.entity.RefundOrder;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerSummaryMapper;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;

import lombok.extern.slf4j.Slf4j;

/**
 * 活动中心-借款返现红包事件
 * 
 * 触发条件：还款
 * 
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Component
public class ActivityPrizeLoanReturnCashNotify implements RefundNotifyHandle {
	
	@Autowired
	private ProjectBorrowerSummaryMapper projectBorrowerSummaryMapper;
	
	@Reference(version = "1.0.0")
	private ActivityPrizeDubboService activityPrizeDubboService;
	
	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
		try {
			UseLoanCashBackReqDTO useLoanCashBackReqDTO = (UseLoanCashBackReqDTO) bulidParam(refundNotifyDTO);
			
			if (useLoanCashBackReqDTO == null) return new RefundNotifyHandleResult(); 
			
			log.info("调用活动中心-借款返现红包事件，ProjectId：{}， 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(useLoanCashBackReqDTO));
			activityPrizeDubboService.useLoancashBackPrize(useLoanCashBackReqDTO);
			log.info("调用活动中心-借款返现红包事件完成 ，ProjectId：{}", refundNotifyDTO.getProject().getProjectId());
			refundNotifyHandleResult = new RefundNotifyHandleResult();
		} catch (Exception e) {
			refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
			log.error("调用活动中心-借款返现红包事件异常，异常原因：{}， 请求参数：{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()), e);
		}
		return refundNotifyHandleResult;
	}

	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
		RefundOrder refundOrder = refundNotifyDTO.getRefundOrder();
		
		// 标的关联的红包没有查到或者不是借款返现红包直接返回
    	log.info("调用活动中心-借款返现红包事件，请求参数:UserId:{}, PrizeId:{}", project.getBorrowUserId(), project.getPrizeId());
		UserPrizeResponseDTO userPrizeResponseDTO = activityPrizeDubboService.getUserPrizeInfo(project.getBorrowUserId(), project.getPrizeId());
        if (userPrizeResponseDTO == null) {
        	log.info("调用活动中心-借款返现红包事件，没有查询到相关红包，UserId:{}, PrizeId:{}", project.getBorrowUserId(), project.getPrizeId());
        	return null;
        }
        log.info("调用活动中心-借款返现红包事件，返回结果:{}", JSONObject.toJSONString(userPrizeResponseDTO));
        
        // 较验红包类型，非借款返现红包直接返回
		if (!UserPrizeTypeIdEnum.LOAN_CASHBACK.getPrizeType().equals(userPrizeResponseDTO.getPrizeTypeId())) {
			return null;
		}
		
		ProjectBorrowerSummary summary = projectBorrowerSummaryMapper.selectByPrimaryKey(project.getProjectId());

		// 构建请求参数
		UseLoanCashBackReqDTO useLoanCashBackReqDTO = new UseLoanCashBackReqDTO();
		useLoanCashBackReqDTO.setUserId(project.getBorrowUserId());
		useLoanCashBackReqDTO.setPrizeId(project.getPrizeId());
		useLoanCashBackReqDTO.setCurrentPeriods(summary.getCurrentPeriod());
		if (RefundTypeEnum.OVERDEPT.getRefundType().equals(refundOrder.getRefundType())) {
			useLoanCashBackReqDTO.setUserPrizeUseSceneEnum(UserPrizeUseSceneEnum.REPAYMENT_ADVANCE);
		}
		return useLoanCashBackReqDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ACTIVITY_PRIZE_LOAN_RETURN_CASH;
	}	
}